﻿<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Setup.aspx.cs" Inherits="setup_Setup" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Apollo application installer</title>
    <link href="../css/smoothness/jquery-ui-1.8.13.custom.css" rel="stylesheet" type="text/css" />
    <style>
        body 
        {
            font-family: Arial, Sans-Serif;
            font-size: 12px;
            font-weight: normal;
            color: #0f0f0f;    
        }
        #tblSteps td 
        {
            padding: 3px;    
        }
    </style>
    <script type="text/javascript" src="../js/jquery-1.5.2.min.js"></script>
    <script type="text/javascript">

        // posible statuses: Pending | PendingUserApprpouve | Done | Failed
        var steps = [
            { description: "Creating tables in the db", code: 2, status: "Pending", message: "" },
            { description: "Generate entities mappings", code: 3, status: "Pending", message: "" },
            { description: "Generate relations", code: 4, status: "Pending", message: "" },

            { description: "Creating default language", code: 5, status: "Pending", message: "Default: ENGLISH" },

            { description: "Create advanced entity management", code: 6, status: "Pending", message: "" },
            { description: "Create user management", code: 7, status: "Pending", message: "" },

            { description: "Creating admin user", code: 8, status: "Pending", message: "" },

            { description: "Creating settings menus", code: 9, status: "Pending", message: "" },

            { description: "Restart Apollo", code: 10, status: "Pending", message: "" }
        ];

        function RunSteps(stps) {
            try { 
            var jqTable = $("#tblSteps");
            jqTable.empty();

            var jqTBody = $("<tbody></tbody>");
            jqTable.append(jqTBody);

            // create header
            jqTBody.append(
            $("<tr></tr>")
                .append($("<th width=\"24px\" nowrap>#</th>"))
                .append($("<th>Description</th>"))
                .append($("<th width=\"18px\" nowrap>&nbsp;</th>"))
                .append($("<th>Message</th>"))
                .addClass("ui-state-default")
            );

            var pendingStep = null;

            // create items
            for (var idx = 0; idx < stps.length; idx++) {
                var statusIcon = "";

                switch (stps[idx].status) {
                    case "Pending":
                        statusIcon = "<img src=\"../css/apollo/loading16x16.gif\" />";

                        if (pendingStep == null)
                            pendingStep = stps[idx];

                        break;
                    case "PendingUserApprpouve":
                        statusIcon = "<img src=\"../css/apollo/loading16x16.gif\" />";

                        if (pendingStep == null)
                            pendingStep = stps[idx];

                        break;
                    case "Done": statusIcon = "<img src=\"../css/icons/icon_77.png\" />"; break;
                    case "Failed": statusIcon = "<img src=\"../css/icons/system.png\" />"; break;
                }

                jqTBody.append(
                    $("<tr></tr>")
                        .append($("<td></td>").html((idx + 1)))
                        .append($("<td></td>").html(stps[idx].description))
                        .append($("<td></td>").html(statusIcon))
                        .append($("<td></td>").html(stps[idx].message))
                        .addClass("ui-widget-content ui-row-ltr")
                );
            }

            // run first step that is pending
            if (pendingStep != null)
                RunStep(pendingStep);
        } catch (ex) { alert(ex); }
        }

        function RunStep(stp) {
            if (stp.status == "PendingUserApprpouve") {
                if (!confirm("Do you want to run step: \"" + stp.description + "\"")) {
                    stp.status = "Failed";
                    stp.message = "Canceled by user."

                    RunSteps(steps);
                    
                    return;
                }
            }

            $.ajax({
                url: "SetupAsync.ashx",
                data: ("code=" + stp.code),
                success: function (ajaxMsg) {
                    var jsonMsg = $.parseJSON(ajaxMsg);

                    stp.message = jsonMsg.message;
                    stp.status = jsonMsg.status;

                    RunSteps(steps);
                }
            });
        }


        $(document).ready(function () {
            RunSteps(steps);
        });

    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div style="text-align: right;">
        <a href="/" title="Apollo">Goto Apollo</a> | 
        <a href="/" title="Help">Help?</a>
        <table id="tblSteps" style="margin: auto; text-align: left" class="ui-widget ui-corner-all ui-state-default"></table>
    </div>
    </form>
</body>
</html>
 